using System;
using System.Runtime.CompilerServices;
using Engine;
using Engine.Graphics;

namespace Game
{
	public class LJGameScreen : GameScreen
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class _003C_003Ec
		{
			public static readonly _003C_003Ec _003C_003E9 = new _003C_003Ec();

			public static Action _003C_003E9__2_0;

			internal void _003C_002Ector_003Eb__2_0()
			{
				GameManager.SaveProject(true, true);
			}
		}

		private new double m_lastAutosaveTime;

		private bool m_waitingForPlayer;

		public LJGameScreen()
		{
			Children.Clear();
			LJGamesWidget lJGamesWidget = new LJGamesWidget();
			lJGamesWidget.Name = "GamesWidget";
			Children.Add(lJGamesWidget);
			base.IsDrawRequired = true;
			Window.Deactivated += _003C_003Ec._003C_003E9__2_0 ?? (_003C_003Ec._003C_003E9__2_0 = new Action(_003C_003Ec._003C_003E9._003C_002Ector_003Eb__2_0));
		}

		public override void Enter(object[] parameters)
		{
			if (GameManager.Project != null)
			{
				GameManager.Project.FindSubsystem<SubsystemAudio>(true).Unmute();
			}
			MusicManager.CurrentMix = MusicManager.Mix.None;
			if (ScreensManager.PreviousScreen is PlayerScreen)
			{
				m_waitingForPlayer = true;
				return;
			}
			m_waitingForPlayer = false;
			NetworkManager.ClientStage = NetworkManager.PeerStage.Ready;
		}

		public override void Leave()
		{
			NetworkManager.Manager.DisconnectAll();
			NetworkManager.ClientStage = NetworkManager.PeerStage.NotReady;
			if (GameManager.Project != null)
			{
				GameManager.Project.FindSubsystem<SubsystemAudio>(true).Mute();
				GameManager.SaveProject(true, true);
			}
			ShowHideCursors(true);
			MusicManager.CurrentMix = MusicManager.Mix.Menu;
		}

		public override void Update()
		{
			if (GameManager.Project != null)
			{
				double realTime = Time.RealTime;
				if (realTime - m_lastAutosaveTime > 120.0)
				{
					m_lastAutosaveTime = realTime;
					GameManager.SaveProject(false, true);
				}
				GameManager.UpdateProject();
				if (m_waitingForPlayer)
				{
					ReadOnlyList<PlayerData> playersData = GameManager.Project.FindSubsystem<SubsystemPlayers>().PlayersData;
					bool flag = true;
					foreach (PlayerData item in playersData)
					{
						flag &= item.IsReadyForPlaying;
					}
					if (flag || playersData.Count == 0)
					{
						m_waitingForPlayer = false;
						NetworkManager.ClientStage = NetworkManager.PeerStage.Ready;
					}
				}
			}
			ShowHideCursors(GameManager.Project == null || DialogsManager.HasDialogs((ContainerWidget)this) || DialogsManager.HasDialogs(((Widget)this).RootWidget) || ScreensManager.CurrentScreen != this);
		}

		public virtual void Draw()
		{
			if (!ScreensManager.IsAnimating && SettingsManager.ResolutionMode == ResolutionMode.High)
			{
				Display.Clear(Color.Black, 1f, 0);
			}
		}

		private new void ShowHideCursors(bool show)
		{
			base.Input.IsMouseCursorVisible = show;
			base.Input.IsPadCursorVisible = show;
		}
	}
}
